//
// Created by 77873 on 2020/7/16 0016.
//

#ifndef FFMPEGPROJECT_MINEAUDIO_H
#define FFMPEGPROJECT_MINEAUDIO_H

#include "MineQueue.h"
#include "MinePlayStatus.h"
#include "MineCallJava.h"
#include "soundtouch/include/SoundTouch.h"

#include "WlBufferQueue.h"
#include "PcmDataBean.h"
//#include "SoundTouch.h"


using namespace soundtouch;

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
#include <libavutil/time.h>
}


class MineAudio {


public:

    /**
     * 流的索引
     */
    int streamIndex = -1;

    AVCodecParameters *codecParameters = NULL;

    AVCodecContext *avCodecContext = NULL;

    MineQueue *mineQueue = NULL;

    MinePlayStatus *playStatus = NULL;

    MineCallJava *callJava = NULL;

    /**
     * 播放器线程
     */
    pthread_t threadPlay;

    AVPacket *avPacket = NULL;

    AVFrame *avFrame = NULL;


    int ret = -1;


    uint8_t *buffer = NULL;   //无符号8位类型
    int data_size = 0;

    int sample_Reate = 0;
    int duration = 0;//时常
    AVRational time_base;
    double now_time;
    double clock;
    double last_time = 0;
    //音量大小
    int volumePercent = 50;
    //初始化声道为立体声
    int mute = 2;

    //变速值
    float pitch = 1.0f;
    //变调值
    float speed = 1.0f;

    /**
     * 引擎接口
     */
    SLObjectItf engineObject = NULL;
    SLEngineItf enginEngine = NULL;

    /**
     * 混音器
     */
    SLObjectItf outputMixObject = NULL;
    SLEnvironmentalReverbItf outputMixEnviromentalReverb = NULL;
    SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;

    /**
     * pcm
     */
    SLObjectItf pcmPlayerObject = NULL;
    SLPlayItf pcmPlayerPlay = NULL;
    //声音
    SLVolumeItf slVolumeItf = NULL;
    //声道切换
    SLMuteSoloItf pcmMutePlay = NULL;

    /**
     * 缓冲器队列接口
     */
    SLAndroidSimpleBufferQueueItf pcmBufferQueue = NULL;


    //soundtouch
    SoundTouch *soundTouch = NULL;
    SAMPLETYPE *sampleBuffer = NULL;
    bool finished = true;
    uint8_t *out_buffer = NULL;
    int nb;
    int num = 0;//存储返回的采样个数

    //录制开关

    bool isRecordPcm = false;

    //如果一帧内含有多个Frame 循环读取 读取完成 为true
    bool readFrameFinished = true;

    //
    bool isCut = false;
    int end_time = 0;
    bool showPcm = false;

    //分包线程
    pthread_t pcmCallBackThread;
    WlBufferQueue *bufferQueue;
    int defaultPcmSize = 4096;

public:

    MineAudio(MinePlayStatus *status, MineCallJava *callJava, int sample_Reate);

    ~MineAudio();


    void play();

    void pause();

    void resume();

    void stop();

    void release();

    /**
     * 重采样方法（返回重采样的大小）
     */
    int resampleAudio(void **pcmbuf);


    void initOpenSL();

    void createEngine();

    void createBufferQueueAudioPlayer();

    int getCurrentSampleRateForOpensles(int sample_rate);

    void setVolume(int percent);

    void setMute(int mute);

    int getSoundTouchData();


    void setPitch(float pitch);

    void setSpeed(float speed);

    int getPCMDB(char *pcmdata, size_t pcmsize);

    void startStopRecord(bool start);
};


#endif //FFMPEGPROJECT_MINEAUDIO_H
